package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.exception.ServiceException;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteMediaTag;
import com.cz.video.website.domain.bo.WebsiteMediaTagBo;
import com.cz.video.website.domain.vo.WebsiteMediaTagVo;
import com.cz.video.website.mapper.WebsiteMediaTagMapper;
import com.cz.video.website.mapper.WebsiteVideoTagMapper;
import com.cz.video.website.service.IWebsiteMediaTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 电影标签表 服务实现类
 * </p>
 *
 * @author 洪圆胜
 * @since 2023-10-15
 */
@Service
@RequiredArgsConstructor
public class WebsiteMediaTagServiceImpl extends ServiceImpl<WebsiteMediaTagMapper, WebsiteMediaTag> implements IWebsiteMediaTagService {

    private final WebsiteMediaTagMapper tagMapper;
    private final WebsiteVideoTagMapper videoTagMapper;

    /**
     * 新增电影标签
     * @param websiteMediaTag
     * @return
     */
    @Override
    public int addMediaTag(WebsiteMediaTagBo websiteMediaTag) {
        checkNameUnique(websiteMediaTag);
        return tagMapper.insert(websiteMediaTag);
    }

    private void checkNameUnique(WebsiteMediaTagBo websiteMediaTag) {
        boolean exists = tagMapper.exists(Wrappers.<WebsiteMediaTag>lambdaQuery()
            .eq(websiteMediaTag.getTagId() != null, WebsiteMediaTag::getTagId, websiteMediaTag.getTagId())
            .eq(WebsiteMediaTag::getContent, websiteMediaTag.getContent()));
        if (exists) {
            throw new ServiceException("标签名不能重复");
        }
    }

    @Override
    public int update(WebsiteMediaTagBo websiteMediaTag) {
        checkNameUnique(websiteMediaTag);
        return tagMapper.updateById(websiteMediaTag);
    }

    @Override
    public int removeById(Long id) {
        return tagMapper.deleteById(id);
    }

    @Override
    public WebsiteMediaTagVo getById(Long id) {
        return tagMapper.selectDetailInfo(id);
    }

    @Override
    public TableDataInfo<WebsiteMediaTagVo> page(WebsiteMediaTagBo websiteMediaTag, PageQuery pageQuery) {
        Page<WebsiteMediaTagVo> page= tagMapper.selectPageList(buildQueryWrapper(websiteMediaTag), pageQuery.build());
        return TableDataInfo.build(page);
    }

    private Wrapper<WebsiteMediaTag> buildQueryWrapper(WebsiteMediaTagBo websiteMediaTagBo) {
        Map<String, Object> params = websiteMediaTagBo.getParams();
        QueryWrapper<WebsiteMediaTag> wrapper = Wrappers.query();
        wrapper.eq("1", 1)
            .like(StringUtils.isNotBlank(websiteMediaTagBo.getContent()), "wmt.content", websiteMediaTagBo.getContent())
            .eq(ObjectUtil.isNotNull(websiteMediaTagBo.getCategoryId()), "wmt.category_id", websiteMediaTagBo.getCategoryId())
            .eq(ObjectUtil.isNotNull(websiteMediaTagBo.getStatus()), "wmt.status", websiteMediaTagBo.getStatus())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wmt.create_time", params.get("beginTime"), params.get("endTime"))
        ;
        return wrapper;
    }
}
